package com.passbook.merchant.controller;

import com.passbook.common.domain.BusinessException;
import com.passbook.common.domain.CommonErrorCode;
import com.passbook.common.util.PhoneUtil;
import com.passbook.common.util.Response;
import com.passbook.merchant.api.MerchantService;
import com.passbook.merchant.api.dto.MerchantDTO;
import com.passbook.merchant.api.dto.PassTemplateDTO;
import com.passbook.merchant.convert.DropPassTemplateConvert;
import com.passbook.merchant.convert.MerchantRegisterConvert;
import com.passbook.merchant.security.AuthorizationToken;
import com.passbook.merchant.vo.MerchantRegisterVO;
import com.passbook.merchant.vo.PassTemplateVO;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "商户平台接口", tags = "商户平台接口", description = "商户平台")
public class MerchantController {


    @org.apache.dubbo.config.annotation.Reference
    MerchantService merchantService;

    @ApiOperation(value = "根据ID查询商户")
    @GetMapping("/passbookmerchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        MerchantDTO merchantdto = merchantService.queryMerchantById(id);
        return merchantdto;
    }

    @ApiOperation(value = "商户注册")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true,
            dataType = "MerchantRegisterVO", paramType = "body")
    @PostMapping("/passbookmerchants/create")
    public MerchantRegisterVO createMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        // 校验前端传来的数据
        if (merchantRegisterVO == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        if (StringUtils.isBlank(merchantRegisterVO.getPhone())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }

        // 校验手机号
        if (!PhoneUtil.isMatches(merchantRegisterVO.getPhone())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        // 校验联系人非空
        if (StringUtils.isBlank(merchantRegisterVO.getName())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        // 校验验证码非空
        if (StringUtils.isBlank(merchantRegisterVO.getAddress())) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }

        if (StringUtils.isBlank(merchantRegisterVO.getLogoUrl())) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }
        if (StringUtils.isBlank(merchantRegisterVO.getBusinessLicenseUrl())) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }

        // 校验验证码
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegisterVO);
        merchantService.createMerchant(merchantDTO);
        return merchantRegisterVO;
    }

//    @AuthorizationToken
    @ApiOperation(value = "商户投放优惠券")
    @ApiImplicitParam(name = "DropPassTemplate", value = "优惠券信息", required = true,
            dataType = "PassTemplateVO", paramType = "body")
    @PostMapping("/droppasstemplate")
    public Response DropPassTemplate(@RequestBody PassTemplateVO passTemplateVO) {
        // 校验参数
        if (passTemplateVO == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        // TODO 校验前端传来的参数

        PassTemplateDTO passTemplateDTO = DropPassTemplateConvert.INSTANCE.vo2dto(passTemplateVO);
        Response response = merchantService.dropPassTemplate(passTemplateDTO);
        return response;
    }

}
